package demo11

// 迭代设计模式

import "fmt"
// 迭代器集合

type Aggregate interface{
	Iterator() Iterator
}

// 迭代器 规则

type Iterator interface{
	First()
	IsDone() bool
	Next() interface{}
}
// 迭代的对象 数据结构
type Numbers struct{
	start,end int
}

func NewNumbers(start,end int) *Numbers{
	return &Numbers{
		start:start,
		end:end,
	}
}

func (n *Numbers)Iterator()Iterator{
	return &NumberIterator{
		next:n,
	}
}
// 迭代器的实现
type NumberIterator struct{
	next *Numbers
	start int
}

func (n *NumberIterator)First(){
	n.start = n.next.start
}

func (n *NumberIterator)IsDone() bool{
	return n.start > n.next.end
}

func (n *NumberIterator)Next()interface{}{
	if !n.IsDone(){
		next := n.start
		n.start++
		return next
	}
	return nil
}

func IteratorPrint(i Iterator){
	for i.First();!i.IsDone();{
		res := i.Next()
		fmt.Println(res)
	}
}